<template>
  <div class="app-container">
    <el-card v-show="showSearch" class="mb10" shadow="hover">
      <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="120px" size="small">
        <el-form-item label="油品简称" prop="params.fuelName">
          <el-input
            v-model="queryParams.params.fuelName"
            clearable
            placeholder="请输入油品简称"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item class="ml10">
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="hover">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['base:fuel:add']"
            icon="el-icon-plus"
            plain
            size="mini"
            type="primary"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['base:fuel:edit']"
            :disabled="single"
            icon="el-icon-edit"
            plain
            size="mini"
            type="success"
            @click="handleUpdate"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['base:fuel:remove']"
            :disabled="multiple"
            icon="el-icon-delete"
            plain
            size="mini"
            type="danger"
            @click="handleDelete"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['base:fuel:export']"
            icon="el-icon-download"
            plain
            size="mini"
            type="warning"
            @click="handleExport"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="fuelList" border @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column v-if="true" align="center" label="ID" prop="id" sortable/>
        <el-table-column align="center" label="油品简称" prop="fuelName"/>
        <el-table-column align="center" label="油品全称" prop="fullName"/>
        <el-table-column align="center" label="颜色代码" prop="colorCode">
          <template #default="scope">
            <div :style="{'background-color':scope.row.colorCode}" class="color-item">&nbsp;&nbsp;</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建时间" prop="createDate" sortable>
          <template slot-scope="scope">
            <span>{{ formatDateTime(scope.row.createDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="编辑时间" prop="modifiedDate" sortable>
          <template slot-scope="scope">
            <span>{{ formatDateTime(scope.row.modifiedDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="备注" prop="remark"/>
        <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
          <template slot-scope="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                v-hasPermi="['base:fuel:edit']"
                icon="el-icon-edit-outline"
                size="mini"
                type="text"
                @click="handleUpdate(scope.row)"/>
            </el-tooltip>

            <el-tooltip content="删除" placement="top">
              <el-button
                v-hasPermi="['base:fuel:remove']"
                icon="el-icon-delete"
                size="mini"
                type="text"
                @click="handleDelete(scope.row)"/>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :limit.sync="queryParams.pageSize"
        :page.sync="queryParams.pageNum"
        :total="total"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改油品基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="油品简称" prop="fuelName">
          <el-input v-model="form.fuelName" placeholder="请输入油品简称"/>
        </el-form-item>
        <el-form-item label="油品全称" prop="fullName">
          <el-input v-model="form.fullName" placeholder="请输入油品全称"/>
        </el-form-item>
        <el-form-item label="颜色代码" prop="colorCode">
          <el-color-picker v-model="fuelColor"></el-color-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" :rows="2" placeholder="备注" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addFuel, getFuel, listFuel, updateFuel} from "@/api/base/fuel";
import {formatDateTime, nowCnFormatDateTime} from "@/utils/date";
import {background} from "quill/ui/icons";

export default {
  name: "Fuel",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 油品基础信息表格数据
      fuelList: [],
      // 弹出层标题
      title: "",
      // 油品颜色
      fuelColor: "#409EFF",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params: {
          fuelName: undefined,
          fullName: undefined,
          colorCode: undefined,
          remark: undefined
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fuelName: [
          {
            required: true, message: "油品简称不能为空", trigger: "blur"
          }
        ],
        fullName: [
          {
            required: true, message: "油品全称不能为空", trigger: "blur"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    background,
    formatDateTime,
    /** 查询油品基础信息列表 */
    getList() {
      this.loading = true;
      listFuel(this.queryParams).then(response => {
        this.fuelList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    handleSortChange(data) {
      console.log('handleSortChange')
      console.log(data)
      if (data.order) {
        if (data.order.indexOf('asc') > -1) {
          this.queryParams.sortOrder = 'asc'
        } else {
          this.queryParams.sortOrder = 'desc'
        }
        this.queryParams.sortName = data.prop
      }
      console.log(this.queryParams)
      this.getList()
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fuelName: undefined,
        fullName: undefined,
        colorCode: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加油品基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getFuel(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.fuelColor = this.form.colorCode;
        this.open = true;
        this.title = "修改油品基础信息";
      });
    },
    /** 提交按钮 */
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          this.form.colorCode = this.fuelColor;
          if (this.form.id != null) {
            updateFuel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addFuel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除油品基础信息编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delFuel(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/fuel/export', {
        ...this.queryParams
      }, `fuel_${nowCnFormatDateTime()}.xlsx`)
    }
  }
};
</script>

<style>

.color-item {
  margin-left: 40%;
  width: 30px;
  height: 30px;
  border-radius: 50%;
}

</style>
